import { fileURLToPath, URL } from 'node:url'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: env.VITE_BASE_URL,
    server: {
      port: 8080,
      host: true,
      open: true,
      proxy: {
        // 1. 优先处理登录接口，代理到 7000 端口
        '/dev-api/login': {
          target: 'http://localhost:7000',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev-api/, '') // 移除 /dev-api 前缀
        },
        // https://cn.vitejs.dev/config/#server-proxy
        '/dev-api': {
          target: 'http://localhost:7001',
          // target: 'https://api.wzs.pub/mock/13',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, '')
        },
        '/profile': {
          target: 'http://localhost:7000',
          changeOrigin: true,
        },
        '/upload': {
          target: 'http://localhost:7000',
          changeOrigin: true,
        }, '/baidu_api': {
          target: 'https://api.map.baidu.com',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/baidu_api/, '')
        }, '/chat': {
          target: 'ws://localhost:7000',
          // target: 'https://api.wzs.pub/mock/13',
          changeOrigin: true
        }
      }
    },
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
      },
    },

  }

})
